package com.fastloan.safe.mgt.user.utils;

import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.apache.commons.lang3.StringUtils;

/**
 * @author rankai
 *         createTime 2017-11-2017/11/27 10:03
 */
public class UserUtil {

    public static ResultTo validExistUserBean(CmuUser user, CmuUserService userService, boolean isUpdate) {
        CmuUser cmuUser = new CmuUser();
        int selectCount;
        CmuUser primaryKey = null;
        if (isUpdate) {
            primaryKey = userService.selectByPrimaryKey(user.getId());
        }
        if (StringUtils.isNotBlank(user.getUsername())) {
            if (isUpdate) {
                if (!primaryKey.getUsername().equals(user.getUsername())) {
                    return new ResultTo(ResultEnum.OPERATION_FAILED, "用户名不能更改");
                }
            } else {
                cmuUser.setUsername(user.getUsername());
                selectCount = userService.selectCount(cmuUser);
                if (selectCount != 0) {
                    return new ResultTo(ResultEnum.OPERATION_FAILED, "用户名已经存在");
                }
            }
        }
        if (StringUtils.isNotBlank(user.getCode())) {
            if (isUpdate) {
                if (!primaryKey.getCode().equals(user.getCode())) {
                    return new ResultTo(ResultEnum.OPERATION_FAILED, "用户编号不能更改");
                }
            } else {
                cmuUser.setUsername(null);
                cmuUser.setCode(user.getCode());
                selectCount = userService.selectCount(cmuUser);
                if (selectCount != 0) {
                    return new ResultTo(ResultEnum.OPERATION_FAILED, "用户编号已经存在");
                }
            }
        }
        if (StringUtils.isNotBlank(user.getPhone())) {
            if (isUpdate) {
                if (!primaryKey.getPhone().equals(user.getPhone())) {
                    cmuUser.setUsername(null);
                    cmuUser.setPhone(user.getPhone());
                    selectCount = userService.selectCount(cmuUser);
                    if (selectCount != 0) {
                        return new ResultTo(ResultEnum.OPERATION_FAILED, "手机号已经存在");
                    }
                }
            } else {
                cmuUser.setUsername(null);
                cmuUser.setPhone(user.getPhone());
                selectCount = userService.selectCount(cmuUser);
                if (selectCount != 0) {
                    return new ResultTo(ResultEnum.OPERATION_FAILED, "手机号已经存在");
                }
            }
        }
        if (StringUtils.isNotBlank(user.getEmail())) {
            if (isUpdate) {
                if (!primaryKey.getEmail().equals(user.getEmail())) {
                    cmuUser.setPhone(null);
                    cmuUser.setEmail(user.getEmail());
                    selectCount = userService.selectCount(cmuUser);
                    if (selectCount != 0) {
                        return new ResultTo(ResultEnum.OPERATION_FAILED, "邮箱经存在");
                    }
                }
            } else {
                cmuUser.setPhone(null);
                cmuUser.setEmail(user.getEmail());
                selectCount = userService.selectCount(cmuUser);
                if (selectCount != 0) {
                    return new ResultTo(ResultEnum.OPERATION_FAILED, "邮箱经存在");
                }
            }
        }
        if (StringUtils.isNotBlank(user.getIdCard())) {
            if (isUpdate) {
                if (!primaryKey.getIdCard().equals(user.getIdCard())) {
                    return new ResultTo(ResultEnum.OPERATION_FAILED, "身份证不能修改");
                }
            } else {
                cmuUser.setEmail(null);
                cmuUser.setIdCard(user.getIdCard());
                selectCount = userService.selectCount(cmuUser);
                if (selectCount != 0) {
                    return new ResultTo(ResultEnum.OPERATION_FAILED, "身份证已经存在");
                }
            }
        }
        return new ResultTo();
    }
}
